//
//  DeviceModelRepository.swift
//  
//
//  Created by 孙长坦 on 2023/3/13.
//

import Foundation
import LibBase

class DeviceModelRepository: DataRepository<DeviceModel> {
    public override class var id: String {
        return "DeviceModelRepository"
    }
    
    public required init() {
        let cacheData: DeviceModel? = ServiceManager.shared.cacheService.globalCache
            .object(type: DeviceModel.self, key: DeviceModelRepository.id)
        
        let data: DeviceModel
        if let cacheData = cacheData {
            data = cacheData
        } else {
            data = DeviceModel(id: UUID().uuidString, date: Date())
            ServiceManager.shared.cacheService
                .globalCache
                .setObject(value: data, key: DeviceModelRepository.id)
        }
        
        super.init(data: data)
    }
    
    public override func save() {
        guard let saveData = self.data else {
            ServiceManager.shared.cacheService
                .globalCache
                .remove(key: DeviceModelRepository.id)
            return
        }
        
        ServiceManager.shared.cacheService
            .globalCache
            .setObject(value: saveData, key: DeviceModelRepository.id)
    }
}
